<?php
/**
 * ScoreController.class.php
 * 积分控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-03-01
 */
namespace Admin\Controller;

class ScoreController extends BaseController {
    /**
     * 积分获取记录
     * @param   $p  页码数
     * @param   $time_type  时间段
     * @param   $keyword    关键词
     * @return  $list(json)
     */
    function getList(){
        $page   = $this->iInfo['p'] ? $this->iInfo['p'] : 1;
        $type   = $this->iInfo['type'];
        $keyword    = $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;
        $timeE  = time();
        switch($this->iInfo['time_type']){
            case 1: $timeS  = strtotime('-7 days'); break;
            case 2: $timeS  = strtotime('-30 days'); break;
            default : $timeE    = false; $timeS     = false; break;
        }

        $pageHtml   = true;
        $list   = D('Score')->getList($type, $keyword, $timeS, $timeE, $page, $pageHtml);
        if($list === false)
            $this->outOInfo(D('Score')->errMsg, 0);
        $this->outOInfo(array('list' => $list, 'page_html' => $pageHtml));
    }

    /**
     * 积分导出
     * @param   $start 开始位置，参数存在执行到处操作，否则返回条目数
     * @param   $length     到处长度
     * @param   $keyword 关键词
     * @param   $time_type 事件类型
     * @param   $type   
     * @return  json [<description>]
     */
    function outExcel(){
        $start   = isset($this->iInfo['start']) ? $this->iInfo['start'] : false;
        $length     = $this->iInfo['length'] ? $this->iInfo['length'] : false;
        $keyword    = $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;
        $type   = $this->iInfo['type'] ? $this->iInfo['type'] : false;
        $timeType   = $this->iInfo['time_type'];
        switch($timeType){
            case 1:
                $timeS  = strtotime('-7 days');
                $timeE  = time();
            break;
            case 2:
                $timeS  = strtotime('-30 days');
                $timeE  = time();
            break;
            default:
                $timeS  = false;
                $timeE  = false;
            break;
        }

        $result     = D('Score')->outExcel($type, $keyword, $timeS, $timeE, $start, $length);
        if($start === false)
            $this->outOInfo(array('count' => $result));
        $title  = array(
            '积分ID', '用户ID', '用户昵称', '真实姓名', 
            '积分类型', '积分数量', '获取时间', '类型'
        );
        foreach($result as &$item){
            $data['id'] = $item['id'];
            $data['user_id']    = $item['user_id'];
            $data['nickname']   = $item['nickname'];
            $data['truename']   = $item['truename'];
            $data['type_text']  = C('SCORE_USE_TYPE.'.$item['goods_type']);
            $data['score']  = $item['score'];
            $data['create_date']    = date('Y-m-d H:i:s', $item['create_time']);
            $data['score_type']     = $item['score'] > 0 ? '收入' : '支出';
            $item   = $data; 
        }
        $excelName  = 'score_list_'.$start;
        out_excel($title, $result, $excelName);
    }
}
